﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InputKeyboard.Control
{
    public static class ControlProperty
    {
        #region 控件圆角度
        public static CornerRadius GetControlCornerRadius(DependencyObject obj)
        {
            return (CornerRadius)obj.GetValue(ControlCornerRadiusProperty);
        }
        public static void SetControlCornerRadius(DependencyObject obj, CornerRadius value)
        {
            obj.SetValue(ControlCornerRadiusProperty, value);
        }
        public static readonly DependencyProperty ControlCornerRadiusProperty = DependencyProperty.RegisterAttached("ControlCornerRadius", typeof(CornerRadius), typeof(ControlProperty), new PropertyMetadata(null));
        #endregion

        #region 提示
        public static string GetTips(DependencyObject obj)
        {
            return (string)obj.GetValue(TipsProperty);
        }
        public static void SetTips(DependencyObject obj, string value)
        {
            obj.SetValue(TipsProperty, value);
        }
        public static readonly DependencyProperty TipsProperty = DependencyProperty.RegisterAttached("Tips", typeof(string), typeof(ControlProperty), new PropertyMetadata(null));
        #endregion

        #region 单位
        public static string GetUnit(DependencyObject obj)
        {
            return (string)obj.GetValue(UnitProperty);
        }
        public static void SetUnit(DependencyObject obj, string value)
        {
            obj.SetValue(UnitProperty, value);
        }
        public static readonly DependencyProperty UnitProperty = DependencyProperty.RegisterAttached("Unit", typeof(string), typeof(ControlProperty), new PropertyMetadata(null));

        #endregion

        #region 标记
        public static bool GetShowSign(DependencyObject obj)
        {
            return (bool)obj.GetValue(ShowSignProperty);
        }
        public static void SetShowSign(DependencyObject obj, bool value)
        {
            obj.SetValue(ShowSignProperty, value);
        }
        public static readonly DependencyProperty ShowSignProperty = DependencyProperty.RegisterAttached("ShowSign", typeof(bool), typeof(ControlProperty), new PropertyMetadata(false));

        #endregion

        #region 图标图片
        public static Geometry GetIcon(DependencyObject obj)
        {
            return (Geometry)obj.GetValue(IconProperty);
        }
        public static void SetIcon(DependencyObject obj, Geometry value)
        {
            obj.SetValue(IconProperty, value);
        }
        public static readonly DependencyProperty IconProperty = DependencyProperty.RegisterAttached("Icon", typeof(Geometry), typeof(ControlProperty), new PropertyMetadata(null));


        public static Geometry GetSubIcon(DependencyObject obj)
        {
            return (Geometry)obj.GetValue(SubIconProperty);
        }
        public static void SetSubIcon(DependencyObject obj, Geometry value)
        {
            obj.SetValue(SubIconProperty, value);
        }
        public static readonly DependencyProperty SubIconProperty = DependencyProperty.RegisterAttached("SubIcon", typeof(Geometry), typeof(ControlProperty), new PropertyMetadata(null));

        public static ImageSource GetImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(ImageProperty);
        }
        public static void SetImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(ImageProperty, value);
        }
        public static readonly DependencyProperty ImageProperty = DependencyProperty.RegisterAttached("Image", typeof(ImageSource), typeof(ControlProperty), new PropertyMetadata(null));

        public static ImageSource GetSubImage(DependencyObject obj)
        {
            return (ImageSource)obj.GetValue(SubImageProperty);
        }
        public static void SetSubImage(DependencyObject obj, ImageSource value)
        {
            obj.SetValue(SubImageProperty, value);
        }
        public static readonly DependencyProperty SubImageProperty = DependencyProperty.RegisterAttached("SubImage", typeof(ImageSource), typeof(ControlProperty), new PropertyMetadata(null));

        #endregion

        #region 图标颜色
        public static Brush GetIconBrush(DependencyObject obj)
        {
            return (Brush)obj.GetValue(IconBrushProperty);
        }

        public static void SetIconBrush(DependencyObject obj, Brush value)
        {
            obj.SetValue(IconBrushProperty, value);
        }
        public static readonly DependencyProperty IconBrushProperty = DependencyProperty.RegisterAttached("IconBrush", typeof(Brush), typeof(ControlProperty), new PropertyMetadata(Brushes.Gray));
        #endregion

        #region 密码输入框
        public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(ControlProperty), new PropertyMetadata(false, Attach));
        public static void SetAttach(DependencyObject dp, bool value)
        {
            dp.SetValue(AttachProperty, value);
        }
        public static bool GetAttach(DependencyObject dp)
        {
            return (bool)dp.GetValue(AttachProperty);
        }

        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(ControlProperty), new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));
        public static string GetPassword(DependencyObject dp)
        {
            return (string)dp.GetValue(PasswordProperty);
        }
        public static void SetPassword(DependencyObject dp, string value)
        {
            dp.SetValue(PasswordProperty, value);
        }

        private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(ControlProperty));
        private static bool GetIsUpdating(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsUpdatingProperty);
        }
        private static void SetIsUpdating(DependencyObject dp, bool value)
        {
            dp.SetValue(IsUpdatingProperty, value);
        }

        private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (sender is PasswordBox passwordBox)
            {
                passwordBox.PasswordChanged -= PasswordChanged;
                if (!GetIsUpdating(passwordBox)) passwordBox.Password = (string)e.NewValue;
                passwordBox.PasswordChanged += PasswordChanged;
            }
        }
        private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (!(sender is PasswordBox passwordBox)) return;
            if ((bool)e.OldValue) passwordBox.PasswordChanged -= PasswordChanged;
            if ((bool)e.NewValue) passwordBox.PasswordChanged += PasswordChanged;
        }
        private static void PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (sender is PasswordBox passwordBox)
            {
                SetIsUpdating(passwordBox, true);
                SetPassword(passwordBox, passwordBox.Password);
                SetIsUpdating(passwordBox, false);
            }
        }
        #endregion

        #region 图标控件排序
        public static Orientation GetOrientation(DependencyObject obj)
        {
            return (Orientation)obj.GetValue(OrientationProperty);
        }
        public static void SetOrientation(DependencyObject obj, Orientation value)
        {
            obj.SetValue(OrientationProperty, value);
        }
        public static readonly DependencyProperty OrientationProperty = DependencyProperty.RegisterAttached("Orientation", typeof(Orientation), typeof(ControlProperty), new PropertyMetadata(Orientation.Horizontal));
        #endregion

        #region 子控件高度百分比
        public static double GetSubHeightPercent(DependencyObject obj)
        {
            return (double)obj.GetValue(SubHeightPercentProperty);
        }
        public static void SetSubHeightPercent(DependencyObject obj, double value)
        {
            obj.SetValue(SubHeightPercentProperty, value);
        }
        public static readonly DependencyProperty SubHeightPercentProperty = DependencyProperty.RegisterAttached("SubHeightPercent", typeof(double), typeof(ControlProperty), new PropertyMetadata(1d));


        #endregion

        #region 文本内容

        public static string GetText(DependencyObject obj)
        {
            return (string)obj.GetValue(TextProperty);
        }
        public static void SetText(DependencyObject obj, string value)
        {
            obj.SetValue(TextProperty, value);
        }
        public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached("Text", typeof(string), typeof(ControlProperty), new PropertyMetadata(""));

        public static string GetShiftText(DependencyObject obj)
        {
            return (string)obj.GetValue(ShiftTextProperty);
        }
        public static void SetShiftText(DependencyObject obj, string value)
        {
            obj.SetValue(ShiftTextProperty, value);
        }
        public static readonly DependencyProperty ShiftTextProperty = DependencyProperty.RegisterAttached("ShiftText", typeof(string), typeof(ControlProperty), new PropertyMetadata(""));


        public static bool GetIsShift(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsShiftProperty);
        }
        public static void SetIsShift(DependencyObject obj, bool value)
        {
            obj.SetValue(IsShiftProperty, value);
        }
        public static readonly DependencyProperty IsShiftProperty = DependencyProperty.RegisterAttached("IsShift", typeof(bool), typeof(ControlProperty), new PropertyMetadata(false));


        public static bool GetIsDown(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsDownProperty);
        }
        public static void SetIsDown(DependencyObject obj, bool value)
        {
            obj.SetValue(IsDownProperty, value);
        }
        public static readonly DependencyProperty IsDownProperty = DependencyProperty.RegisterAttached("IsDown", typeof(bool), typeof(ControlProperty), new PropertyMetadata(false));

        #endregion
    }
}
